﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThietBi.BLL;
using QuanLyThietBi.DAL.Models;

namespace QuanLyThietBi
{
    public partial class LichSuThietBi : Form
    {
        private ThietBiBO tbBO;
        private NhanVienBO nvBO;
        private int thietbiId = -1;

        public LichSuThietBi()
        {
            InitializeComponent();
        }

        public LichSuThietBi(int thietbiID)
        {
            thietbiId = thietbiID;
            InitializeComponent();
        }

        private void LichSuThietBi_Load(object sender, EventArgs e)
        {
            var thietbi = GetThietBi();
            if (thietbi != null)
            {
                lblTenThietBi.Text = thietbi.ten_thiet_bi;
                lblMaThietBi.Text = thietbi.ma_thiet_bi;
                lblTinhTrang.Text = GetTenTinhTrang(thietbiId);
                lblbPhongBan.Text = GetTenPhongBan(thietbiId);
                lblNamMua.Text = thietbi.nam_mua;
                BindDataToDataGridview();
            }


        }

        private void BindDataToDataGridview()
        {
            LichSuThietBiBO lstb = new LichSuThietBiBO();
            List<LICHSUTHIETBI> listLichSuThietBi = lstb.GetAllLichSuThietBi(thietbiId);
            if (listLichSuThietBi != null && listLichSuThietBi.Count > 0)
            {
                DataTable table = new DataTable();
                table.Columns.Add("STT", typeof(int));
                table.Columns.Add("Thời gian", typeof(string));
                table.Columns.Add("Nội dung", typeof(string));
                table.Columns.Add("Mô tả", typeof(string));
                table.Columns.Add("Tình trạng thiết bị", typeof(string));
                table.Columns.Add("Đề xuất sửa chữa", typeof(string));
                table.Columns.Add("Ngày bàn giao thiết bị", typeof(string));

                int i = 1;
                foreach (var tb in listLichSuThietBi)
                {
                    table.Rows.Add(i++,
                        tb.ngay,
                        tb.noi_dung,
                        tb.mo_ta,
                        tb.tinh_trang_thiet_bi,
                        tb.de_xuat_sua_chua,
                        tb.ngay_ban_giao);
                }
                this.dtgLichSuThietBi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dtgLichSuThietBi.DataSource = table;
            }
        }

        //private string GetTenThietBi()
        //{
        //    ThietBiBO tbBO = new ThietBiBO();
        //    THIETBI tb = tbBO.GetThietBiById(thietbiId);
        //    if (tb != null)
        //    {
        //        return tb.ten_thiet_bi;
        //    }
        //    else
        //    {
        //        return string.Empty;
        //    }
        //}

        private THIETBI GetThietBi()
        {
            ThietBiBO tbBO = new ThietBiBO();
            THIETBI tb = tbBO.GetThietBiById(thietbiId);
            if (tb != null)
            {
                return tb;
            }
            return null;
        }

        private string GetTenTinhTrang(int thietbiId)
        {
            TinhTrangBO ttBO = new TinhTrangBO();
            return ttBO.GetTenTinhTrang(thietbiId);
        }

        private string GetTenPhongBan(int thietbiId)
        {
            string tenphongban = string.Empty;
            PhongBanBO pbBO = new PhongBanBO();
            nvBO = new NhanVienBO();
            NHANVIEN nv = nvBO.GetNhanVien(thietbiId);
            if (nv != null)
            {
                tenphongban = pbBO.GetTenPhongBan(nv.id);
            }
            return tenphongban;
        }

        private void btnTroLai_Click(object sender, EventArgs e)
        {
            ThongKe tk = new ThongKe();
            tk.Show();
            this.Hide();
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            Main home = new Main();
            home.Show();
            this.Hide();
        }
    }
}
